//
//  SDCollectionView.m
//  SuperDoctor
//
//  Created by 林森 on 16/7/9.
//  Copyright © 2016年 SD. All rights reserved.
//

#import "SDCollectionView.h"

@interface SDCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic,weak)UINavigationController *nav;


@end

@implementation SDCollectionView

- (instancetype)newCollecttionView:(UICollectionViewFlowLayout * )flowlayou array:(NSMutableArray *)array{
    
    SDCollectionView * collec = [[SDCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowlayou];
    
    
    collec.array = array;

    
    
    return collec;
    
}

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        [self maekCollection];
    }
    
    return self;
}

- (void)maekCollection{
    
    
    
    self.delegate = self;
    
    self.dataSource = self;
    
    [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.item == self.array.count) {
        
    }
    JASidePanelController *jas= (JASidePanelController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    
    UINavigationController *nav = (UINavigationController *)jas.centerPanel;
    
    _nav = nav;
    
    //MARK: - 获取相册图片
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    //类型
    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //代理
    pickerController.delegate = self;
    //跳转
    [nav presentViewController:pickerController animated:YES completion:nil];
    
    
}

#pragma mark - PickerController获取图片的代理
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    //MARK: - 取出从相册里选择的图片
    UIImage *selectImage = info[UIImagePickerControllerOriginalImage];
    //设置为添加图片按钮的背景图
    [self.array addObject:selectImage];


    
    //跳出
    [_nav dismissViewControllerAnimated:YES completion:nil];
    
    

    
    [self reloadData];
    

}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    

    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return self.array.count + 1;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    UIImageView * image = [[UIImageView alloc]init];
    

    
    if (indexPath.item == self.array.count) {
        image.image = [UIImage imageNamed:@"compose_pic_add_highlighted"];
    }else{
    
            image.image = self.array[indexPath.item];
    
    }
    
    [image sizeToFit];
    
    cell.backgroundView = image;
    
    
    return cell;
    
}

//- (NSMutableArray<UIImage *> *)array{
//
//    if (_array == nil) {
//        _array = [NSMutableArray array];
//    }
//    return _array;
//}


@end
